package itsur.arquitecturas.implicitinvocation;

/**************************************************************************************
** Class name: StudentReader
** Author: A.J. Lattanze
** Date: 12/29/99
** Version 1.0
***************************************************************************************
** Purpose: Assignment 2 for SE 17-655, Architectures for Software Systems.  This class
** will read from the InputFile and instantiate the Course objects in the
** system.  It is assumed that the InputFile:
**
**	1) is in the local directory
**	2) contains the active students
**	3) each line of input is read and expected in the following format
**
**		The input file, Students.txt is a field oriented and space-separated text
**		file that lists all of the students.  
**		The fields are as follows:
**
**		324123123 Franklin John MSE 12323 21323 12123
**		|		|	 |	|	|	|	|
**		|		|	 |	|	Courses taken by this student
**		|		|	 |	Program Affiliation
**		|		|	 Student's First Name
**		|		Student's Last Name
**		Social Security
**
** The Students.txt file has been provided as an example file for you to use.
**
** Abstract:
**
**	private Student ParseStudentText( String LineOfText ) - The purpose of the this 
** 	method is to parse lines of text that are read from the text file containing	
**	student information in the above format.  Note that this is a private method and
**	it is used exclusively by GetStudentList
**	
**	public StudentList GetStudentList() - This method reads a line of text.  The
**	line of text is passed to the ParseStudentText method where it is parsed and a 
**	Student object is returned.  The Student object is then added to the list.  When
**	a null is read from the Student file the method terminates and returns the list
** 	to the caller.  A list that points to null is an empty list.
**
** Modification Log
**************************************************************************************
** M1 - Bug in ParseStudentText
** Author: G.A. Lewis
** Date: 01/25/2001
** Description: There was a bug in ParseStudentText that was causing it not to read
** the last course into the list of courses taken.
**************************************************************************************/

public class StudentReader extends LineOfTextFileReader
{
	StudentList ListOfStudents = new StudentList();
	
	private Student ParseStudentText( String LineOfText )
	{
	   	boolean Done = false;	// Indicates the end of parsing
		String Token;		// String token parsed from the line of text
		int TokenCount = 0;	// Number of tokens parsed
		int FrontIndex = 0;	// Front index or character position
		int BackIndex = 0;	// Rear index or character position

		// Create a student object to record all of the info parsed from
		// the line of text

		Student NewStudent = new Student();

		while( !Done )
		{
		   BackIndex = LineOfText.indexOf( ' ', FrontIndex );

		   if ( BackIndex == -1 )
		   {
			Done = true;
			Token = LineOfText.substring( FrontIndex );
		
		   } else {
				
		      Token = LineOfText.substring( FrontIndex, BackIndex );
	       }
	       
		   switch ( TokenCount )
		   	{
			   case 0: 	// Student Social Security Number
					NewStudent.StudentID = Token;
			 		FrontIndex = BackIndex + 1;
					TokenCount ++;
					break;

			   case 1: 	// Student's last name
					NewStudent.LastName = Token;
				  	FrontIndex = BackIndex + 1;
				  	TokenCount ++;
				  	break;

			   case 2: 	// Student's First name
					NewStudent.FirstName = Token;
					FrontIndex = BackIndex + 1;
				  	TokenCount ++;
				  	break;

			   case 3: 	// Program affiliation (e.g. MSE)
					NewStudent.Program = Token;
				  	FrontIndex = BackIndex + 1;
				  	TokenCount ++;
				  	break;

			   default:	// This is where the courses are added to the student'
					// courses-taken list within the student object
					// Note that there are no details other than the course
					// ID that is recorded in the student's courses-taken
					// list.  
					NewStudent.CoursesTakenList.AddCourse(new Course(Token));
				  	FrontIndex = BackIndex + 1;
				  	break;

		      } // end switch
		   } // end while

		return( NewStudent );

 	} // ParseStudentText


	public StudentList GetStudentList( String InputFile )
	{
		String StudentText;	// Line of text from the Students.txt file
		boolean Done;		// End of the file - stop processing

		// New student list object - this will contain all of the students in the
		// Students.txt file

		StudentList ListObject = new StudentList();

		if ( OpenFile ( InputFile ) )
		{
		   Done = false;

		   while ( !Done ) 
	 	   {
			try
			{
  			   StudentText = ReadLineOfText();

			   if ( StudentText == null ) 
			   {
				Done = true;

			   } else {

				ListObject.AddStudent( ParseStudentText(StudentText) );

			   } // if 

			} // try

			catch( Exception Error )
			{
		 	   return( null );

			} // catch
		
		   } // while		

		} else {
			return( null );

		} // if

		return( ListObject );

	} // GetStudentList


	StudentReader()
	{
		ListOfStudents = null;

	} // Constructor #1

	StudentReader( String InputFile )
	{
		ListOfStudents = GetStudentList( InputFile );

	} // Constructor #2

} // StudentReader